package pjn;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class CharactersEvaluator {

    private Splitter splitter = new Splitter();
    private GramaticalResolver gramaticalResolver = new GramaticalResolver();

    public void findPositiveAndNegativeSentences(Set<Character> characters, List<String> sentences, Map<Integer, Set<Integer>> charactersInSentencesCount) {
        for (Character character : characters) {
            for (String name : character.getNames()) {
                for (Integer sentenceNumber : character.getNamesOccurenceSentences().get(name)) {
                    if (charactersInSentencesCount.get(1).contains(sentenceNumber)) {
                        int index = sentences.get(sentenceNumber).indexOf(name);
                        boolean recognized;

                        List<String> words = splitter.splitIntoWords(sentences.get(sentenceNumber).substring(0, index));
                        for (int i = words.size() - 1; i >= 0; i--) {
                            recognized = false;
                            PartOfSpeech partOfSpeech = gramaticalResolver.resolvePartOfSpeach(words.get(i));
                            if (words.get(i).equals("-")) {
                                break;
                            } else if (partOfSpeech == null) {
                                continue;
                            } else {
                                GenderAndNumber gan = gramaticalResolver.resolveGenderAndNumber(words.get(i), partOfSpeech);
                                if (gan == character.getGender()) {
                                    int[] ids = JCLP.rec(words.get(i));
                                    if (KnowledgeBase.POSITIVE_QUALITIES.contains(JCLP.bform(ids[0]).trim())) {
                                        character.addPositiveSentence(sentenceNumber);
                                        recognized = true;
                                    } else if (KnowledgeBase.NEGATIVE_QUALITIES.contains(JCLP.bform(ids[0]).trim())) {
                                        character.addNegativeSentence(sentenceNumber);
                                        recognized = true;
                                    }
                                }
                                if (!recognized && partOfSpeech == PartOfSpeech.NOUN) {
                                    break;
                                }
                            }
                        }

                        words = splitter.splitIntoWords(sentences.get(sentenceNumber).substring(index + name.length() + 1));
                        for (int i = 0; i < words.size(); i++) {
                            recognized = false;
                            PartOfSpeech partOfSpeech = gramaticalResolver.resolvePartOfSpeach(words.get(i));
                            if (words.get(i).equals("-")) {
                                break;
                            } else if (partOfSpeech == null) {
                                continue;
                            } else {
                                GenderAndNumber gan = gramaticalResolver.resolveGenderAndNumber(words.get(i), partOfSpeech);
                                if (gan == character.getGender()) {
                                    int[] ids = JCLP.rec(words.get(i));
                                    if (KnowledgeBase.POSITIVE_QUALITIES.contains(JCLP.bform(ids[0]).trim())) {
                                        character.addPositiveSentence(sentenceNumber);
                                        recognized = true;
                                    } else if (KnowledgeBase.NEGATIVE_QUALITIES.contains(JCLP.bform(ids[0]).trim())) {
                                        character.addNegativeSentence(sentenceNumber);
                                        recognized = true;
                                    }
                                }
                                if (!recognized && partOfSpeech == PartOfSpeech.NOUN) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
